package com.company.biz.user.action;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.biz.user.service.UserService;
import com.company.common.exception.BizException;
import com.company.common.util.ImportExcelUtils;
import com.company.user.entity.UserEntity;
import com.company.user.req.*;
import com.company.user.resp.UserInfoResp;
import com.company.user.vo.GeneralResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(description = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户列表分页")
    @RequestMapping(value = "/getUserListPage", method = RequestMethod.POST)
    public GeneralResult<Page<UserInfoResp>> getUserListPage(@RequestBody UserListPageReq req ) {
        UserEntity userEntity = getUserEntity();
        Page<UserInfoResp> userListPage = userService.getUserListPage(req,userEntity);
        return GeneralResult.genSuccessResult(userListPage);
    }

    @ApiOperation(value = "添加员工")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public GeneralResult<String> addUser(@RequestBody AddUserReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = userService.addUser(req,userEntity);
        return b ? GeneralResult.genSuccessResult("添加成功"):GeneralResult.genErrorResult("添加失败");
    }

    @ApiOperation(value = "通过userId获取员工信息")
    @RequestMapping(value = "/getUserInfoByUserId", method = RequestMethod.POST)
    public GeneralResult<UserInfoResp> getUserInfoByUserId(@RequestBody GetUserInfoByUserIdReq req) {
        UserEntity userEntity = getUserEntity();
        UserInfoResp userInfoResp= userService.getUserInfoByUserId(req,userEntity);
        return GeneralResult.genSuccessResult(userInfoResp);
    }
    @ApiOperation(value = "通过userId修改员工信息")
    @RequestMapping(value = "/updateUserInfoByUserInfo", method = RequestMethod.POST)
    public GeneralResult<String> updateUserInfoByUserInfo(@RequestBody UpdateUserInfoByUserInfoReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = userService.updateUserInfoByUserInfo(req, userEntity);
        return b ? GeneralResult.genSuccessResult("修改成功"):GeneralResult.genErrorResult("修改失败");
    }
    @ApiOperation(value = "通过userId删除员工信息")
    @RequestMapping(value = "/deleteUserInfoByUserIds", method = RequestMethod.POST)
    public GeneralResult<String> deleteUserInfoByUserIds(@RequestBody DeleteUserInfoByUserIdsReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = userService.deleteUserInfoByUserIds(req, userEntity);
        return b ? GeneralResult.genSuccessResult("删除成功"):GeneralResult.genErrorResult("删除失败");
    }

    @ApiOperation(value = "重置密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public GeneralResult<String> resetPassword(@RequestBody UserIdReq req) {
        return GeneralResult.genSuccessResult(userService.resetPassword(req.getUserId())? "修改成功 密码为888888" : "修改失败");
    }

}
